$(function () {
	search.init();
});

function locdau(str) {
	str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
	str = str.replace(/À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ/g, "A");
	str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
	str = str.replace(/È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ/g, "E");
	str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
	str = str.replace(/Ì|Í|Ị|Ỉ|Ĩ/g, "I");
	str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
	str = str.replace(/Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ/g, "O");
	str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
	str = str.replace(/Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ/g, "U");
	str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
	str = str.replace(/Ỳ|Ý|Ỵ|Ỷ|Ỹ/g, "Y");
	str = str.replace(/đ/g, "d");
	str = str.replace(/Đ/g, "D");
	str = str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g, "-");
	str = str.replace(/-+-/g, "-");
	str = str.replace(/^\-+|\-+$/g, "");
	return str;
}

function GetSearchResults(page, categoryIDs, colors, productName) {
	$("div.productContent div.loading").css("width", $("div.productContent").width());
	$("div.productContent div.loading").css("height", $("div.productContent").height());
	$("div.productContent div.loading").css("display", "block");
	$.ajax({
		url: "../ajax/ajax.SearchProduct.php?page=" + page + "&category_IDs=" + categoryIDs + "&colors=" + colors + "&productName=" + productName,
		type: "GET",
		dataType: "html",
		cache: false,
		success: function (data) {
			$("div.productContent").html(data);
			$("ul.pager > li > a").click(function () {
				GetSearchResults($(this).attr("page"), categoryIDs, colors, productName);
            });
			$("div.productContent div.loading").css("display", "none");
		}
	});
}

var search = {
	textBox_Click: function () {
		$(":text.searchbox").click(function () {
			$(this).val("");
		}),
        
        $(":text.searchbox").blur(function () {
			if($.trim($(this).val()) == "")
                $(this).val("- Tên sản phẩm -");
		});
	},

	ddl_Click: function () {
		$("img#btnCategory").click(function () {
			if ($("#CategoryFilterContain").css("display") == "none") {
				if ($.trim($("#clbCategory").html()) == "") {
					$("div#search div.loading").css("width", $("div#search").width());
					$("div#search div.loading").css("height", $("div#search").height());
					$("div#search div.loading").css("display", "block");
					$.ajax({
						url: "../ajax/ajax.GetCategoriesInSearchBox.php",
						type: "GET",
						dataType: "html",
						cache: false,
						success: function (data) {
							$("#clbCategory").html(data);
							$(".cbCategory").click(function () {
								UpdateCategoryFilter($(this).attr("checked"), $(this).attr("value"));
							});
							$("div#search div.loading").css("display", "none");
							$("#CategoryFilterContain").slideDown(200);
						}
					});
				}
				else
					$("#CategoryFilterContain").slideDown(200);
			}
			else
				$("#CategoryFilterContain").slideUp(200);
		});
	},

	CheckAllCategory_Click: function () {
		$("#imgCheckAll_Category").click(function () {
			$(".cbCategory").attr("checked", true);
			var str = "";
			$.each($(".cbCategory"), function (index, term) {
				if (index == 0)
					str += $(this).attr("value");
				else
					str += ',' + $(this).attr("value");
			});
			document.getElementById('txtCategory').value = str;
		});
	},

	UnCheckCategory_Click: function () {
		$("#imgUncheck_Category").click(function () {
			$(".cbCategory").attr("checked", false);
			document.getElementById('txtCategory').value = '- Any -';
		});
	},

	ApplyCategory_Click: function () {
		$("#imgApply_Category").click(function () {
			$("#CategoryFilterContain").css("display", "none");
		});
	},

	BtnSearch_Click: function () {
		$(":button.btnSearch").click(function () {
			var ids_Category = "", productName = "", colors = "";
			$(":checked.cbCategory").each(function (index, term) {
				if (index == 0)
					ids_Category += $(term).attr("categoryID");
				else
					ids_Category += "|" + $(term).attr("categoryID");
			});
			if (ids_Category == "")
				ids_Category = "-Any-";
			productName = $(":text.searchbox").val();
			if (productName == "- Tên sản phẩm -")
				productName = "-Any-";
			else
				productName = locdau(productName);
			$("div#colorBarContain :checked").each(function (index, term) {
				if (index == 0)
					colors += $(term).val();
				else
					colors += "|" + $(term).val();
			});
			if (colors == "")
				colors = "-Any-";
			location.href = "../views/Search.php?page=1&category_IDs=" + ids_Category + "&colors=" + colors + "&productName=" + productName;
		});
	},

	init: function () {
		search.BtnSearch_Click();
		search.textBox_Click();
		search.ddl_Click();
		search.CheckAllCategory_Click();
		search.UnCheckCategory_Click();
		search.ApplyCategory_Click();
	}
}

function OnClickColor(id) {
	if (document.getElementById('cb' + id).checked == false) {
		document.getElementById('cb' + id).checked = true
		document.getElementById('cross' + id).style.display = 'block';
	}
	else {
		document.getElementById('cb' + id).checked = false;
		document.getElementById('cross' + id).style.display = 'none';
	}
}

function UpdateCategoryFilter(checked, value) {
	if (document.getElementById('txtCategory').value == '- Any -')
		document.getElementById('txtCategory').value = "";
	if (checked) {
		if (document.getElementById('txtCategory').value == "")
			document.getElementById('txtCategory').value += value;
		else
			document.getElementById('txtCategory').value += ',' + value;
	}
	else {
		var str = "";
		var arr = document.getElementById('txtCategory').value.split(',');
		for (i = 0; i < arr.length; i++) {
			if (arr[i] != value) {
				if (str != "")
					str += ',' + arr[i];
				else
					str += arr[i];
			}
		}
		if (str == "")
			document.getElementById('txtCategory').value = '- Any -';
		else
			document.getElementById('txtCategory').value = str;
	}
}

function GetDataNew(page){
        $("div.loading").css("width", $("div.productContent").width());
	$("div.loading").css("height", $("div.productContent").height());
	$("div.productContent div.loading").css("display", "block");
        $.ajax({
            url: "../ajax/ajax.NewProduct.php?page=" + page,
		type: "GET",
		dataType: "html",
		cache: false,
		success: function (data) {
			$("div.productContent").html(data);
			$("ul.pager > li > a").click(function () {
				GetDataNew($(this).attr("page"));
             });
             $("div.productContent div.loading").css("display", "none");
                }
        });
}

function GetDataMostSold(page){
        $("div.productContent div.loading").css("width", $("div.productContent").width());
	$("div.productContent div.loading").css("height", $("div.productContent").height());
	$("div.productContent div.loading").css("display", "block");
        $.ajax({
            url: "../ajax/ajax.MostSoldProduct.php?page=" + page,
		type: "GET",
		dataType: "html",
		cache: false,
		success: function (data) {
			$("div.productContent").html(data);
			$("ul.pager > li > a").click(function () {
				GetDataMostSold($(this).attr("page"));
             });
             $("div.productContent div.loading").css("display", "none");
                }
        });
}